package com.coment.dto;

import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.gift.GiftWorthCfg;
import com.coment.enums.ApiPushType;
import com.coment.po.UserInfo;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.mapping.MongoId;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/// 平台消息{礼物|评论|点赞}
@EqualsAndHashCode
@ToString(exclude = {"retryCount", "status", "giftId"})
@AllArgsConstructor
@Data
@NoArgsConstructor
@Slf4j
public class ApiNotify implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    private Integer retryCount = 0;
    private Integer status = 0;

    @MongoId
    /// 消息id
    private String messageId;

    /// 主播id
    private Integer anchorInfoId;

    /// 直播平台
    private LiveData.ApiType apiType;

    /// 房间id
    private String roomId;

    /// 推送类型
    private String pushType;

    /// 推送环境
    private boolean test;

    /// 接收时间
    private Long receiveTime;

    /// 礼物id
    private String giftId;

    /// 礼物名称
    private String giftName;

    /// 礼物数量
    private int giftCount;

    /// 礼物单价
    private int giftUnitPrice;

    /// 礼物总价
    private int giftTotalPrice;

    /// 点赞数量
    private int likeCount;

    /// 弹幕内容
    private String content;

    /// 弹幕用户
    private UserInfo userInfo;

    public ApiNotify(LiveData.DyPayload.CommentsMsg commentsMsg) {
        LiveData.DyPayload.BaseMsg baseMsg = commentsMsg.obtainBaseMsg();
        this.initBaseMsg(baseMsg);
        this.setPushType(ApiPushType.comment.getPushType().getValue());
        this.setContent(commentsMsg.getContent());
    }

    public ApiNotify(LiveData.DyPayload.LiveLikeMsg liveLikeMsg) {
        LiveData.DyPayload.BaseMsg baseMsg = liveLikeMsg.obtainBaseMsg();
        this.initBaseMsg(baseMsg);
        this.setPushType(ApiPushType.like.getPushType().getValue());
        this.setLikeCount(liveLikeMsg.getLike_num());
    }

    public ApiNotify(LiveData.DyPayload.LiveGiftMsg liveGiftMsg) {
        LiveData.DyPayload.BaseMsg baseMsg = liveGiftMsg.obtainBaseMsg();
        this.initBaseMsg(baseMsg);
        this.setPushType(ApiPushType.gift.getPushType().getValue());
        this.setGiftName(Tables.cfg.get_tbgiftworthcfg().get(liveGiftMsg.getSec_gift_id()).name);
        this.setGiftId(liveGiftMsg.getSec_gift_id());
        this.setGiftCount(liveGiftMsg.getGift_num());
        this.setGiftTotalPrice(liveGiftMsg.getGift_value());
        this.setGiftUnitPrice(liveGiftMsg.getGift_value() / liveGiftMsg.getGift_num());
    }

    public ApiNotify(LiveData.DyPayload.FansClubMsg fansClubMsg) {
        LiveData.DyPayload.BaseMsg baseMsg = fansClubMsg.obtainBaseMsg();
        this.initBaseMsg(baseMsg);
        this.setPushType(ApiPushType.fansclub.getPushType().getValue());
    }

    public static List<ApiNotify> toApiNotifyList(LiveData.KSPayload ksPayload) {
        List<ApiNotify> list = new ArrayList<>();

        boolean isTest = ksPayload.getEvent().equals("TEST");
        LiveData.KSPayload.KSPayloadData ksPayloadData = ksPayload.getKSPayloadData();
        for (LiveData.KSPayload.KSPayloadData.Payload payload : ksPayloadData.getPayload()) {
            ApiNotify apiNotify = new ApiNotify();

            /// 基础信息
            apiNotify.setMessageId(ksPayloadData.getUniqueMessageid() + ksPayloadData.getPayload().indexOf(payload));
            apiNotify.setRoomId(ksPayloadData.getRoomCode());
            apiNotify.setApiType(LiveData.ApiType.kuaiShou);
            apiNotify.setPushType(ApiPushType.fromToPushType(ksPayloadData.getPushType()).getValue());
            apiNotify.setTest(isTest);
            apiNotify.setReceiveTime(ksPayload.getTimestamp());

            ///  礼物
            String payloadGiftId = payload.getGiftId();
            GiftWorthCfg giftWorthCfg = Tables.cfg.get_tbgiftworthcfg().get(payloadGiftId);
            apiNotify.setGiftId(payloadGiftId);
            apiNotify.setGiftName(giftWorthCfg == null ? "" : giftWorthCfg.name);
            apiNotify.setGiftCount(payload.getGiftCount() == null ? 0 : payload.getGiftCount().intValue());
            apiNotify.setGiftUnitPrice(payload.getGiftUnitPrice() == null ? 0 : payload.getGiftUnitPrice().intValue());
            apiNotify.setGiftTotalPrice(payload.getGiftTotalPrice() == null ? 0 : payload.getGiftTotalPrice().intValue());

            /// 点赞
            apiNotify.setLikeCount(payload.getCount() == null ? 0 : payload.getCount().intValue());

            /// 评论
            apiNotify.setContent(payload.getContent());

            /// 用户
            LiveData.KSPayload.KSPayloadData.Payload.UserInfo payloadUserInfo = payload.getUserInfo();
            UserInfo userInfo = new UserInfo();
            userInfo.setUId(payloadUserInfo.getUserId());
            userInfo.setName(payloadUserInfo.getUserName());
            userInfo.setAvatar(payloadUserInfo.getHeadUrl());
            apiNotify.setUserInfo(userInfo);

            list.add(apiNotify);
        }
        return list;
    }

    public void initBaseMsg(LiveData.DyPayload.BaseMsg baseMsg) {
        String msgId = baseMsg.getMsg_id();
        String secOpenId = baseMsg.getSec_openid();
        String avatarUrl = baseMsg.getAvatar_url();
        String nickname = baseMsg.getNickname();
        Long timestamp = baseMsg.getTimestamp() == null ? System.currentTimeMillis() : baseMsg.getTimestamp();
        UserInfo build = UserInfo.builder().uId(secOpenId).name(nickname).avatar(avatarUrl).build();
        this.setUserInfo(build);
        boolean test = baseMsg.getTest() == null || baseMsg.getTest();
        if (test) {
            this.setMessageId("" + System.currentTimeMillis());
        } else {
            log.debug("msg {} ", baseMsg);
            this.setMessageId(msgId);
        }

        this.setTest(test);
        this.setReceiveTime(timestamp);
    }
}